<template>
  <div class="notice-container">
    <el-card>
      <el-form :inline="true"
               ref="search_data"
               class="demo-form-inline">
        <el-row>
          <el-form-item label="公告内容">
            <el-input v-model="search_data.keyword"
                      placeholder="输入公告内容" />
          </el-form-item>

          <el-form-item>
            <el-button type="primary"
                       @click="onSearch">查询</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </el-card>

    <el-card shadow="never"
             style="margin-top:10px;">
      <div class="main-card">
        <div>
          <span style="font-size:18px;font-weight:900;">公告列表</span>
        </div>
        <div>
          <el-button icon="el-icon-plus"
                     type="primary"
                     @click="handleAdd">新增</el-button>
        </div>
      </div>
      <el-table ref="multipleTable"
                :data="table_data"
                tooltip-effect="dark"
                style="width: 100%">
        <el-table-column type="selection"> </el-table-column>
        <el-table-column prop="title"
                         label="标题">
        </el-table-column>
        <el-table-column prop="content"
                         label="内容">
        </el-table-column>
        <el-table-column prop="start_at"
                         label="开始时间"
                         width="180">
        </el-table-column>
        <el-table-column prop="end_at"
                         label="结束时间"
                         width="180">
        </el-table-column>
        <el-table-column label="操作"
                         width="100">
          <template slot-scope="scope">
            <el-tooltip class="item"
                        effect="dark"
                        content="编辑按钮"
                        placement="top-start">
              <i class="el-icon-edit"
                 @click="handleEdit(scope.row)"
                 style="margin-left:5px"></i>
            </el-tooltip>

            <el-tooltip class="item"
                        effect="dark"
                        content="删除按钮"
                        placement="top-start">
              <i class="el-icon-delete"
                 @click="handleDelete(scope.row.id)"
                 style="margin-left:5px"></i>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <div style="padding: 10px 0; text-align: right;background:#fff;margin-top:10px">
      <el-pagination background
                     :page-size="search_data.limit"
                     layout="total, prev, pager, next"
                     :total="all_count"
                     @current-change="pageChange" />
    </div>
    <el-dialog title="新增/编辑公告"
               :visible.sync="dialogFormVisible"
               width="500px">
      <el-form :model="form"
               label-width="80px">
        <el-form-item label="标题">
          <el-input v-model="form.title"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="form.content"
                    autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="开始时间">
          <el-date-picker v-model="form.start_at"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间">
          <el-date-picker v-model="form.end_at"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择开始时间">
          </el-date-picker>
        </el-form-item>

      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="handlAddData">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getList, addData, delData } from '@/api/notice'

export default {
  data () {
    return {
      search_data: {
        page: 1,
        limit: 15,
        keyword: '',
      },
      all_count: 0,
      table_data: [],
      dialogFormVisible: false,
      form: {
        id: '',
        content: '',
        title: '',
        end_at: '',
        start_at: '',
      },
      multiple_selection: [],
    }
  },
  created () {
    this.fetchData()
  },
  methods: {
    pageChange (val) {
      this.search_data.page = val;
      this.fetchData();
    },
    fetchData () {
      getList(this.search_data).then(response => {
        this.table_data = response.data.data
        this.all_count = response.data.count
      })
    },
    handleAdd () {
      this.form.id = null
      this.form.title = null
      this.form.content = null
      this.form.start_at = null
      this.form.end_at = null
      this.dialogFormVisible = true
    },
    handleEdit (item) {
      this.form.id = item.id
      this.form.title = item.title
      this.form.content = item.content
      this.form.start_at = item.start_at
      this.form.end_at = item.end_at
      this.dialogFormVisible = true
    },
    handleDelete (id) {
      delData({ id: id }).then(response => {
        if (response.code == 1000) {
          this.fetchData()
        }
      })
    },
    handlAddData () {
      addData(this.form).then(response => {
        if (response.code == 1000) {
          this.dialogFormVisible = false
          this.fetchData()
        } else {

        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.notice-container {
  max-width: 1300px;
  margin: 0 auto;
  padding: 30px;
  .main-card {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>
